package org.x.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.x.user.mapper.SysLogsMapper;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.user.entity.SysLogsDO;
import org.x.user.dto.PageResult;
import org.x.user.query.SysLogsQuery;
import org.x.user.service.LogsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 审计日志管理的实现
 *
 * @author 夏包包
 * @date 2025-03-08 13:39:03
 */
@Service
public class LogsServiceImpl extends ServiceImpl<SysLogsMapper,SysLogsDO> implements LogsService {

    @Autowired
    private SysLogsMapper sysLogsMapper;

    @Override
    public PageResult<SysLogsDO> allLogs() {
        QueryWrapper<SysLogsDO> wrapper = Wrappers.query();
        // 默认查询全部，可以根据需要调整分页
        Page<SysLogsDO> page = new Page<>(1, 1000);
        IPage<SysLogsDO> logPage = sysLogsMapper.selectPage(page, wrapper);
        PageResult<SysLogsDO> pageResult = new PageResult<>(logPage);
        return pageResult;
    }

    @Override
    public SysLogsDO logInfo(SysLogsQuery logQuery) {
        SysLogsDO logDO = sysLogsMapper.selectById(logQuery.getLogId());
        return logDO;
    }

    @Override
    public PageResult<SysLogsDO> logsByTenant(SysLogsQuery logQuery) {
        QueryWrapper<SysLogsDO> wrapper = Wrappers.query();
        wrapper.eq("tenant_id", logQuery.getTenantId());
        Page<SysLogsDO> page = new Page<>(logQuery.getPageIndex(), logQuery.getPageSize());
        IPage<SysLogsDO> logPage = sysLogsMapper.selectPage(page, wrapper);
        PageResult<SysLogsDO> pageResult = new PageResult<>(logPage);
        return pageResult;
    }

    @Override
    public PageResult<SysLogsDO> logsByUser(SysLogsQuery logQuery) {
        QueryWrapper<SysLogsDO> wrapper = Wrappers.query();
        wrapper.eq("user_id", logQuery.getUserId());
        Page<SysLogsDO> page = new Page<>(logQuery.getPageIndex(), logQuery.getPageSize());
        IPage<SysLogsDO> logPage = sysLogsMapper.selectPage(page, wrapper);
        PageResult<SysLogsDO> pageResult = new PageResult<>(logPage);
        return pageResult;
    }

    @Override
    public PageResult<SysLogsDO> logsByLevel(SysLogsQuery logQuery) {
        QueryWrapper<SysLogsDO> wrapper = Wrappers.query();
        wrapper.eq("log_level", logQuery.getLogLevel());
        Page<SysLogsDO> page = new Page<>(logQuery.getPageIndex(), logQuery.getPageSize());
        IPage<SysLogsDO> logPage = sysLogsMapper.selectPage(page, wrapper);
        PageResult<SysLogsDO> pageResult = new PageResult<>(logPage);
        return pageResult;
    }

    @Override
    public PageResult<SysLogsDO> logsByTimeRange(SysLogsQuery logQuery) {
        QueryWrapper<SysLogsDO> wrapper = Wrappers.query();
        wrapper.between("log_time", logQuery.getStartTime(), logQuery.getEndTime());
        Page<SysLogsDO> page = new Page<>(logQuery.getPageIndex(), logQuery.getPageSize());
        IPage<SysLogsDO> logPage = sysLogsMapper.selectPage(page, wrapper);
        PageResult<SysLogsDO> pageResult = new PageResult<>(logPage);
        return pageResult;
    }
}
